import Vue from 'vue'
import Vuex from 'vuex'
import user from './user'
import options from './options'
import {Toast, Dialog} from "vant";
// import axios from "@/utils/axios"
import Clipboard from 'clipboard';
import axios from "@/utils/axios";
// import {device, getCookie, getLocalStorage, setCookie} from "@/utils/common";


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isInit: false,
    loading: false,
    publicConfig: {
      uploadBaseUrl: 'http://upload.dustar.cn/client',
    },
    menu: {
      badge: {
        task: 0,
      },
      active: "home",
      map: {
        home: {
          to: '/home',
          title: '排单表',
          name: 'home',
          icon: 'list-switch'
        },
        code: {
          to: '/code',
          title: '二维码',
          name: 'code',
          icon: 'qr'
        },
        task: {
          to: '/task',
          title: '任务工单',
          name: 'task',
          icon: 'todo-list-o',
        },
        job: {
          to: '/job',
          title: '工作业务',
          name: 'job',
          icon: 'edit'
        },
        setting: {
          to: '/setting',
          title: '我的',
          name: 'setting',
          icon: 'manager-o'
        }
      }
    }
  },
  getters: {

  },
  mutations: {

  },
  actions: {
    async initApp({state, commit, dispatch}, {router, route}) {
      state.loading = true;

      const userinfo =  (Object.keys(state.user.userinfo).length > 0) ?
          state.user.userinfo : await dispatch('user/userinfo');
      await dispatch('options/storeMap');
      await dispatch('options/rightList');

      const fullPathArr = route.fullPath.split("/");
      state.menu.active = fullPathArr[1].split("?")[0];
      state.loading = false;
      state.isInit = true;
    },
    hasRight({state}, targetList) {
      return state.user.userinfo['rightList'] ? state.user.userinfo['rightList'].some(right => targetList.includes(right)) : false;
    }
  },
  modules: {
    user,
    options
  }
})
